home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Online / opennap / getopt.c < prev    next >
C/C++ Source or Header  |  2001-06-08  |  810b  |  46 lines

  1. /* Copyright (C) 2000-1 drscholl@users.sourceforge.net
  2.    This is free software distributed under the terms of the
  3.    GNU Public License.  See the file COPYING for details.
  4.  
  5.    $Id: getopt.c,v 1.5 2001/02/15 08:39:45 drscholl Exp $ */
  6.  
  7. #include <string.h>
  8.  
  9. #define EOF -1
  10.  
  11. int optind = 1;
  12. char *optarg;
  13.  
  14. /* -b 500 -v */
  15. int
  16. _getopt (int ac, char **av, const char *opts)
  17. {
  18.     const char *p;
  19.  
  20.     while (optind < ac)
  21.     {
  22.     if (*av[optind] == '-')
  23.     {
  24.         p = strchr (opts, av[optind][1]);
  25.         if (!p)
  26.         return '?';
  27.         if (*(p + 1) == ':')
  28.         {
  29.         /* requires arg */
  30.         optind++;
  31.         if (!av[optind] || *av[optind] == '-')
  32.             return ':';    /* missing argument */
  33.         optarg = av[optind];
  34.         }
  35.         else
  36.         optarg = 0;
  37.         optind++;
  38.         return *p;
  39.     }
  40.     else
  41.         break;
  42.     }
  43.     optarg = 0;
  44.     return EOF;
  45. }
  46.